#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
const int N = 100;
bool lock;

deque<int> wq;//waiting queue
queue<int> bq;//blocking queue

int cas=0,n=0,t1=0,t2=0,t3=0,t4=0,t5=0,Q=0;
vector<string> prg[N];

int vpc[N], var[26];

void run(int index)
{
	int timecnt = Q, v = 0;
	string curstr;
	while(timecnt > 0){
		curstr = prg[index][vpc[index]];//virtuel PC location
		if(curstr[2] == '='){//set variable
			timecnt -= t1;
			v = curstr[4] - '0';
			if(curstr.size() == 6)
				v = v*10 + curstr[5] - '0'; 
			var[curstr[0] - 'a'] = v; 
		} else if(curstr[0] == 'p'){//print
			timecnt -= t2;
			printf("%d: %d\n",index,var[curstr[6]-'a']);
		} else if(curstr[0] == 'l'){//lock
			timecnt -= t3;
			if(lock){
				bq.push(index);
				return;
			} else
				lock = true;
		} else if(curstr[0] == 'u'){//unlock
			lock = false;
			timecnt -= t4;
			if(!bq.empty()){
				int i = bq.front();
				bq.pop();
				wq.push_front(i);
			}
		} else 
			return;
		++vpc[index];
	}
	wq.push_back(index);
}
int main()
{
	scanf("%d\n",&cas);
	while(cas--){
		scanf("%d %d %d %d %d %d %d\n",&n,&t1,&t2,&t3,&t4,&t5,&Q);
		string line;
		for(int i = 1;i <= n;i++){
			prg[i].clear();
			while(getline(cin,line)){
				if(line == "") continue;
				prg[i].push_back(line);
				if(prg[i].back() == "end") break;
			}
			wq.push_back(i);//entering waiting queue
		}
	}
	memset(vpc,0,sizeof(vpc));
	memset(var,0,sizeof(var));
	while(!wq.empty()){
		int curID = wq.front();
		wq.pop_front();
		run(curID);
	}
#if 0
	for(int i=1;i<=n;i++){
		for(auto it = prg[i].begin();it != prg[i].end();it++)
		cout<<*it<<endl;
	}
#endif
	return 0;
}
